﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using B10.Helpers;
using B10.Models;

namespace B10.Controllers
{
    [HandleError]
    public class PublicProfileController : Controller
    {

        IDatabaseRepository _db;

        public PublicProfileController()
            : this(new DatabaseRepository())
        {

        }

        public PublicProfileController(IDatabaseRepository db)
        {
            _db = db;
        }


        //
        // GET: /PublicProfile/

        public ActionResult Index(int id)
        {
            UserProfile profile = _db.GetProfile(id);

            if (profile == null)
                return new FileNotFoundResult();

            return View(profile);
        }

        public ActionResult PageList(int id, int? page)
        {
            const int pageSize = 15;
            var pages = _db.GetActivePages().Where(p => p.ProfileId == id)
                .OrderByDescending(p => p.CreatedDate);

            var paginated = new PaginatedList<UserPage>(pages, page ?? 1, pageSize);
            return View(new PageListViewModel(paginated));
        }

        public ActionResult Comments(int id, int? page)
        {
            const int pageSize = 15;
            var comments = _db.GetAll<Comment>().Where(p => p.ProfileId == id)
                .OrderByDescending(p => p.Date);

            var paginated = new PaginatedList<Comment>(comments, page ?? 1, pageSize);
            return View(paginated);
        }

    }
}
